﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Module_Album_Controls_AlbumAE : System.Web.UI.UserControl
{
    public Guid AlbumId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["AlbumId"] != null)
        {
            AlbumId = new Guid(Request["AlbumId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else AlbumId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadData();
        }

    }
    private void LoadData()
    {
        TblAlbum item = TblAlbumRepository.CreateInstant().SelectOne(AlbumId);
        if (item != null)
        {
            txtName.Text = item.Name;
            cbIsVisible.Checked = item.IsVisible;
            cbIsPublish.Checked = item.IsPublish;
            if (!item.ImageUrl.Equals(string.Empty))
                Image1.ImageUrl = item.ImageUrl;
            else Image1.Visible = false;
            lblCreatedBy.Text = item.CreatedBy;
            lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
            lblUpdatedBy.Text = item.UpdatedBy;
            lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
        }
    }
    private TblAlbum GetTblAlbum()
    {
        TblAlbum obj = new TblAlbumRepository().SelectOne(AlbumId);
        if (obj == null)
        {
            obj = new TblAlbum();
            obj.Id = Guid.NewGuid();
            obj.TextId = new TblAlbumRepository().GetUniqueTextIdFromUnicodeText(txtName.Text, 250);
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
            obj.DisplayOrder = 100;
            obj.IsPublish = false;
            obj.XmlFullPath = "";
            obj.Views = 0;
            obj.ImageUrl = "";
        }
        obj.Name = txtName.Text;
        string StrSaveImage = SaveImage(FileUpload1.PostedFile, obj.TextId);
        if (!StrSaveImage.Equals(string.Empty))
            obj.ImageUrl = StrSaveImage;
        obj.IsVisible = cbIsVisible.Checked;
        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (AlbumId.Equals(Guid.Empty))
            new TblAlbumRepository().Insert(GetTblAlbum());
        else
            new TblAlbumRepository().Update(GetTblAlbum());
        Response.Redirect("AlbumList.aspx");
    }
    string SaveImage(HttpPostedFile file, string TextId)
    {
        string result = string.Empty;
        try
        {
            if (!file.FileName.Equals(string.Empty))
            {
                string FolderName = "userfiles/album/" + TextId;
                string FileName = file.FileName;
                string physicalFolderFullPath = HttpContext.Current.Server.MapPath("/" + FolderName);
                string _ext = "." + file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
                if (!System.IO.Directory.Exists(physicalFolderFullPath)) System.IO.Directory.CreateDirectory(physicalFolderFullPath); // create image folder if not existed
                try
                {
                    System.IO.File.Delete(physicalFolderFullPath + "/" + TextId + _ext);
                }
                catch { }

                file.SaveAs(physicalFolderFullPath + "/" + TextId + _ext);
                result = "/" + FolderName + "/" + TextId + _ext;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error Upload Image " + ex);
        }
        return result;
    }
}
